package com.milk.controller.admin;

import com.milk.dto.CommonPageQueryDTO;
import com.milk.entity.Coupon;
import com.milk.result.PageResult;
import com.milk.result.Result;
import com.milk.service.ICouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController("/adminCouponController")
@RequestMapping("/admin/coupon")
@Slf4j
@CrossOrigin
public class CouponController {

    @Autowired
    private ICouponService couponService;

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @GetMapping("/pageQuery")
    public Result<PageResult> pageQuery(CommonPageQueryDTO dto){
        log.info("分页查询 = {}",dto);
        PageResult pageResult = couponService.selectPageQuery(dto);
        return Result.success(pageResult);
    }

    /**
     * 更新优惠卷数据
     * @param coupon
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Coupon coupon){
        log.info("更新数据 = {}",coupon);
        coupon.setUpdatedAt(LocalDateTime.now());
        couponService.updateById(coupon);
        return Result.success();
    }

    /**
     * 新增优惠卷
     * @param coupon
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Coupon coupon){
        log.info("新增数据 = {}",coupon);
        coupon.setCreatedAt(LocalDateTime.now());
        couponService.save(coupon);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Long> ids){
        log.info("删除优惠卷 = {}",ids);
        couponService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 查看能积分兑换的优惠卷
     * @param dto
     * @return
     */
    @GetMapping("/byIntegral")
    public Result<PageResult> selectByIntegral(CommonPageQueryDTO dto){
        PageResult pageResult = couponService.selectByIntegral(dto);
        return Result.success(pageResult);
    }

    /**
     * 取消这个优惠卷的积分兑换
     * @param id
     * @return
     */
    @DeleteMapping("/deleteIntegral/{id}")
    public Result deleteIntegral(@PathVariable Long id){
        couponService.deleteIntegral(id);
        return Result.success();
    }

    /**
     * 查看能SDK兑换的优惠卷
     * @param dto
     * @return
     */
    @GetMapping("/bySDK")
    public Result<PageResult> selectBySDK(CommonPageQueryDTO dto){
        PageResult pageResult = couponService.selectBySDK(dto);
        return Result.success(pageResult);
    }

    /**
     * 取消这个优惠卷的SDK兑换
     * @param id
     * @return
     */
    @DeleteMapping("/deleteSDK/{id}")
    public Result deleteSDK(@PathVariable Long id){
        couponService.deleteSDK(id);
        return Result.success();
    }

    /**
     * 查看没有积分兑换的优惠卷
     * @param dto
     * @return
     */
    @GetMapping("/byNoIntegral")
    public Result<PageResult> selectByNoIntegral(CommonPageQueryDTO dto){
        PageResult pageResult = couponService.selectByNoIntegral(dto);
        return Result.success(pageResult);
    }

    /**
     * 查看没有SDK兑换的优惠卷
     * @param dto
     * @return
     */
    @GetMapping("/ByNoSDK")
    public Result<PageResult> selectByNoSDK(CommonPageQueryDTO dto){
        PageResult pageResult = couponService.selectByNoSDK(dto);
        return Result.success(pageResult);
    }
}
